package com.wan37.logic.team.util;

import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.logic.team.domain.vo.TeamVO;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import com.wan37.protobuf.message.client2server.game.GameTeam;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-30 16:37
 **/
@Component
public class TeamRouteUtil {
    /**
     * 发送队伍请求消息
     * @param code
     * @param teamVOList
     * @param userId
     */
    public void sendTeamResponse(int code, List<TeamVO> teamVOList, String userId){
        GameTeam.TeamResponse.Builder trb = GameTeam.TeamResponse.newBuilder();
        trb.setCode(code);
        if (teamVOList != null){
            for (TeamVO teamVO :teamVOList){
                GameTeam.Team.Builder tb = GameTeam.Team.newBuilder();
                tb.setCaptain(teamVO.getCaptain());
                tb.setTeamId(teamVO.getTeamId());
                tb.setTeamName(teamVO.getTeamName());
                for (String roleId : teamVO.getUserIdList()){
                    tb.addUserIdList(GameEntity.stringMsg.newBuilder().setStringId(roleId).build());
                }
                trb.addTeam(tb.build());
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.TEAMRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    /**
     * 发送队伍请求消息
     * @param code
     * @param teamVO
     * @param userId
     */
    public void sendTeamResponse(int code, TeamVO teamVO, String userId){
        GameTeam.TeamResponse.Builder trb = GameTeam.TeamResponse.newBuilder();
        trb.setCode(code);
        if (teamVO != null){
            GameTeam.Team.Builder tb = GameTeam.Team.newBuilder();
            tb.setCaptain(teamVO.getCaptain());
            tb.setTeamId(teamVO.getTeamId());
            tb.setTeamName(teamVO.getTeamName());
            for (String roleId : teamVO.getUserIdList()){
                tb.addUserIdList(GameEntity.stringMsg.newBuilder().setStringId(roleId).build());
            }
            trb.addTeam(tb.build());
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.TEAMRESPONSE, trb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }
}
